$(function() {
	layui.use(['form', 'laypage', 'util', 'layer'], function() {
		var form = layui.form;
		form.render();
		var laypage = layui.laypage;
		var util = layui.util
		var layer = layui.layer;
		var total;

		function find_sysUser_list(page, rows) {
			var index;
			$.ajax({
				type: "post",
				url: "http://localhost:8080/ssmService/sysUser/selectPageUserVo",
				data: {
					"queryParams.curr_page": page,
					"queryParams.page_size": rows
				},
				async: false,
				"beforeSend": function() {
					index = layer.load(1, {
						shade: [0.5, "black"]
					});
				},
				"success": function(result) {
					if(result.resultCode == 0) {
						layer.close(index);
						total = result.data.total;
						build_sysUser_list(result);
					} else {
						layer.msg("请求失败,联系管理员", {
							icon: 2,
							time: 1000
						});
					}
				},
				"error": function() {
					layer.msg("请求失败,联系管理员", {
						icon: 2,
						time: 1000
					});
				}

			});
		}

		function build_sysUser_list(result) {
			var userList = result.data.list;
			$(".table_tbody").empty();
			$.each(userList, function(index, item) {
				//存放id的目的是为了删除，或者修改单个记录的时候找那条记录
				var tr = $("<tr></tr>");
				var td1 = $("<td></td>").addClass("childrenBox").append("<input type='checkbox' user_id='" + item.user_id + "' lay-skin='primary'>");
				var td2 = $("<td></td>").append(item.user.account);
				var td3 = $("<td></td>").append(item.user.name);
				var phone = "";
				if(item.user.phone != null) {
					phone = item.user.phone
				}
				var td4 = $("<td></td>").append(phone);
				var address = ""
				if(item.user.address != null) {
					address = item.user.address
				}
				var td5 = $("<td></td>").append(address);
				var birthday = "";
				if(item.user.birthday != null) {
					birthday = item.user.birthday
				}
				var td6 = $("<td></td>").append(util.toDateString(birthday, "yyyy-MM-dd"));
				   var td7="";
				  if(item.user.state==1){
				  	 td7 = $("<td></td>").append("<input type='checkbox' user_id='" + item.user_id + "' name='' lay-skin='switch' checked>");
				  }else{
				  	 td7 = $("<td></td>").append("<input type='checkbox' user_id='" + item.user_id + "' name='' lay-skin='switch'>");
				  }
				
				var td8 = $("<td></td>").append("<a href='#'  user_id='" + item.user_id + "' class='layui-btn layui-btn-sm updateUser'>编辑</a>")
					.append("<a href='#' user_id='" + item.user_id + "' class='layui-btn layui-btn-sm reset'>重置密码</a>")
					.append("<a href='javascript:void(0);' user_id='" + item.user_id + "'  class='layui-btn layui-btn-sm layui-btn-danger delete'>删除</a>");
				tr.append(td1).append(td2).append(td3).append(td4).append(td5)
					.append(td6).append(td7).append(td8).appendTo(".table_tbody");

			});
			form.render();
		}

		//调用获取用户列表的方法。
		find_sysUser_list(1, 10);
		laypage.render({
			elem: "table_page",
			count: total,
			layout: ['count', 'prev', 'page', 'next', 'limit', 'refresh', 'skip'],
			jump: function(obj, first) {
				if(!first) {
					find_sysUser_list(obj.curr, obj.limit);
				}
			}

		});

		$(document).on("click", ".delete", function() {
			var userId = $(this).attr("user_id");
			layer.confirm("您确定删除该条记录吗", function() {

				delete_userByUserId(userId);
			});

		});

		function delete_userByUserId(userId) {
			$.ajax({
				type: "delete",
				url: "http://localhost:8080/ssmService/sysUser/deleteUser/" + userId,
				async: true,
				"success": function(result) {
					if(result.resultCode == 0) {
						layer.msg(result.message, {
							icon: 1,
							time: 1000
						});
						window.location.reload()
					} else {
						layer.msg(result.message, {
							icon: 2,
							time: 1000
						});
					}
				}

			})

		}
		//实现全选效果，表单绑定事件
		form.on("checkbox(parentBox)", function(data) {
			//data.elem。checked判断是否选中
			if(data.elem.checked) {
				$(".childrenBox input[type='checkbox']").prop("checked", true);
			} else {
				$(".childrenBox input[type='checkbox']").prop("checked", false);
			}
			form.render();

		});

		//实现反选 效果吧，表单绑定事件
		form.on("checkbox", function(data) {
			var length = 0; //checkbox的总个数
			var checkLength = 0; //被选中的总个数
			$(".childrenBox input[type='checkbox']").each(function() {
				length++;
				if($(this).is(":checked")) {
					checkLength++;
				}
			});
			if(checkLength == length) {
				$(".parentBox").prop("checked", true);
			} else {
				$(".parentBox").prop("checked", false);
			}
			form.render();
		});

		//批量删除
		$(".batchDelete").click(function() {
			var ids = ""; //要删除的用户id的数组
			var check = 0; //记录删除的条数
			$(".childrenBox input[type='checkbox']").each(function() {
				//如果当前的checkbox被选中，则将的userId的属性值存放到ids的变量中
				if($(this).is(":checked")) {
					ids += $(this).attr("user_id") + ",";
					check++;
				}

			});
			//若选中次数不超过一次，给出错误提示
			if(check>0){
				layer.confirm("你确定要删除" + check + "条记录吗？", function() {
				//将最后一位的逗号给删除。
				delete_userByUserId(ids.substring(0, ids.length - 1));
			});	
			}else{
				layer.msg("不能少于0条记录",{
					icon:2,
					time:1000
				})
			}
		

		});
		
		//账号的开启与禁封
		form.on("switch",function(data){
			var userId=data.elem.getAttribute("user_id");
			change_state(userId);
			
		});
		//修改用户状态
		function change_state(userId){
			$.ajax({
				type:"get",
				url:"http://localhost:8080/ssmService/sysUser/tiggerState/"+userId,
				async:true,
				"success":function(result){
					if(result.resultCode==0){
						layer.msg(result.message,{
							icon:1,
							time:1000
						});
					}
				},
				"error":function(){
				layer.msg("状态切换失败，联系管理员",{
							icon:2,
							time:1000
						});	
				}
			});
			
		}
     //重置密码
      $(document).on("click",".reset",function(){
         var userId=$(this).attr("user_id");
      	rest_password(userId);
      });
		
		function rest_password(userId){
				$.ajax({
				type:"get",
				url:"http://localhost:8080/ssmService/sysUser/resetPassword/"+userId,
				async:true,
				"success":function(result){
					if(result.resultCode==0){
						layer.msg(result.message,{
							icon:1,
							time:1000
						});
					}
				},
				"error":function(){
				layer.msg("重置密码失败，联系管理员",{
							icon:2,
							time:1000
						});	
				}
			});
		}
		$(".addUser").click(function() {
			var index=layer.open({
				type: 2,
				title: '新增用户信息',
				shadeClose: true,
				shade: 0.8,
				area: ['70%', '90%'],
				content: 'addUser.html'
			});
			//实现全屏化
			layer.full(index);
			
		});


     $(document).on("click",".updateUser",function(){
     	var userId=$(this).attr("user_id");
     	var index=layer.open({
				type: 2,
				title: '修改用户信息',
				shadeClose: true,
				shade: 0.8,
				area: ['70%', '90%'],
				content: 'updateUser.html?userId='+userId
			});
			layer.full(index);
     	
     });
	});
})